<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToPwd">
				<view class="menu-item-box">
					<view>修改密码</view>
					<u-icon class="menu-icon" name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleToUpgrade">
				<view class="menu-item-box">
					<view>版本号</view>
					<view class="iconfont icon-refresh menu-icon" style="display: flex;">{{currentVersion}}
						<view v-if="isShow"
							style="width: 10px;height: 10px;background-color: red;margin: 8px;border-radius: 50%;">
						</view>
					</view>
				</view>
			</view>
			<!--      <view class="list-cell list-cell-arrow" @click="handleCleanTmp">
          <view class="menu-item-box">
            <view class="iconfont icon-clean menu-icon"></view>
            <view>清理缓存</view>
          </view>
        </view> -->
			<!-- <u-button class="text-black" type="primary" @click="onLogOut()">退出登录</u-button> -->
		</view>
	</view>
</template>

<script>
	import {
		SystemController
	} from '@/api';
	import {
		getToken
	} from "@/utils";
	import appDialog from '@/utils/dialog.js';
	import projectConfig from '@/core/config'
	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight,
				currentVersion: "",
				isShow: false
			}
		},
		mounted() {
			this.checkForUpdate()
			this.getCurrentVersionCode2();
		},
		methods: {
			async checkForUpdate(val) {
				let obj = {
					apkVersionDtoFilter: {
						status: 1
					}
				}
				let res = await SystemController.getApkVersionList(this, obj);

				let Response = res.dataList[0]

				//获取当前安装包版本号
				const currentVersionCode = await this.getCurrentVersionCodeWay();

				// 对比版本号
				if (Response.latestPersionCode > currentVersionCode) {
					// 显示更新对话框
					if (val) {
						let resObj = JSON.parse(Response.path)[0]
						
						appDialog.show(this.getUploadFileUrl(resObj, resObj.filename), Response.changelog)
					} else {
						this.isShow = true
					}
					// appDialog.show(Response.path, Response.changelog);

				}
			},
			getUploadFileUrl(item, params) {
			
				if (item == null || item.downloadUri == null) {
					return null;
				} else {
			
					const query = {};
					query.Authorization = getToken();
					query.filename = params
			
					return this.buildGetUrl(item.downloadUri, query);
				}
			},
			
			buildGetUrl(actionName, params) {
				const queryString = this.objectToQueryString(params);
				if (actionName != null && actionName !== '') {
					if (actionName.substring(0, 1) === '/') actionName = actionName.substring(1);
			
			
					return (
						projectConfig.baseUrl + actionName + (queryString == null ? '' : '?' + queryString)
					);
				}
			},
			
			objectToQueryString(params) {
				if (params == null) {
					return null;
				} else {
					return Object.keys(params)
						.map(key => {
							if (params[key] !== undefined) {
								return `${key}=${params[key]}`;
							} else {
								return undefined;
							}
						})
						.filter(item => item != null)
						.join('&');
				}
			},
			getCurrentVersionCodeWay() {
				return new Promise((resolve) => {
					//获取当前安装包版本号
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						resolve(parseInt(wgtinfo.versionCode));
					});
				});
			},
			async getCurrentVersionCode2() {
				this.currentVersion = await this.getCurrentVersionCode();

			},
			getCurrentVersionCode() {
				return new Promise((resolve) => {
					//获取当前安装包版本号
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						resolve((wgtinfo.version));
					});
				});
			},
			handleToPwd() {
				uni.navigateTo({
					url: '/pages/my/pwd'
				})
			},
			handleToUpgrade() {
				this.checkForUpdate(1)
				// this.$modal.showToast('模块建设中~')
			},
			handleCleanTmp() {
				this.$modal.showToast('模块建设中~')
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			onLogOut() {
				SystemController.logout(this).then(res => {
					uni.removeStorage({
						key: 'token',
						success(res) {
							uni.reLaunch({
								url: '/pages/login/index'
							});
						}
					})
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #f8f8f8;
	}

	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}

	.menu-list {
		margin: 15px 15px;

		.menu-item-box {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.menu-icon {
				color: #c0c0c0;
				font-size: 16px;
				margin-right: 5px;

			}

			.text-right {
				margin-left: auto;
				margin-right: 34rpx;
				color: #999;
			}
		}
	}

	.list-cell {
		position: relative;
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;
		color: #333;
		padding: 26rpx 30rpx;
	}

	.list-cell:first-child {
		border-radius: 8rpx 8rpx 0 0;
	}

	.list-cell:last-child {
		border-radius: 0 0 8rpx 8rpx;
	}

	.list-cell::after {
		content: '';
		position: absolute;
		border-bottom: 1px solid #eaeef1;
		-webkit-transform: scaleY(0.5) translateZ(0);
		transform: scaleY(0.5) translateZ(0);
		transform-origin: 0 100%;
		bottom: 0;
		right: 0;
		left: 0;
		pointer-events: none;
	}

	.cu-list.menu>.cu-item {
		position: relative;
		display: flex;
		padding: 0 30upx;
		min-height: 100upx;
		background-color: #ffffff;
		justify-content: space-between;
		align-items: center
	}

	.text-black {
		color: white;
		margin: 20px 0;
	}
</style>